﻿using System.Collections.Generic;
using System.Data.Services.Common;

using Cookbook.Common;
using Cookbook.Services.Core.ExceptionHandling;

namespace Cookbook.Services.Core.Domain
{
    [DataServiceKey("Id")]
    public class Recipe : VersionedEntity, IAggregateRoot
    {
        private IList<Ingredient> ingredients = new List<Ingredient>();

        protected Recipe()
        {
        }

        public Recipe(string title, string description)
        {
            if (title.IsNullOrEmpty())
            {
                ValidationContext.Current.Add(new Violation(CookbookRule.RecipeTitleIsRequired)
                { 
                    {"Property", "Title"}
                });
            }

            if (description.IsNullOrEmpty())
            {
                ValidationContext.Current.Add(new Violation(CookbookRule.RecipeDescriptionIsRequired)
                {
                    {"Property", "Description"}
                });
            }

            Title = title;
            Description = description;
        }

        public virtual string Title { get; private set; }

        public virtual string Description { get; private set; }

        public virtual int? Rating { get; private set; }

        public virtual IEnumerable<Ingredient> Ingredients
        {
            get { return ingredients; }
        }

        public virtual void Rate(int rating)
        {
            Rating = rating;
        }
    }
}